package com.generator.sysManage.positionManage.controller;


import com.generator.sysManage.moduleManage.pojo.Module;
import com.generator.sysManage.positionManage.pojo.JMJob;
import com.generator.sysManage.positionManage.service.impl.JMService;
import com.generator.sysManage.sectionManage.pojo.DMDept;
import com.generator.sysManage.sectionManage.service.impl.DMService;
import com.generator.sysManage.userManage.pojo.*;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * JMController
 * @author WJW
 * @Date .....
 * @version ......
 */
@Controller
@RequestMapping("/positionManage")
public class JMController {

    @Autowired
    private JMService jmService;

    public JMService getJmService() {
        return jmService;
    }
    public void setJmService(JMService jmService) {
        this.jmService = jmService;
    }

    //职位列表（分页）
    //搜索
    @RequestMapping("/positionList")
    public String userList(Model model, JMJob jmJob, PageModel<JMJob> pageModel){
        PageModel<JMJob> userList = jmService.getJMJobListByFY(jmJob,pageModel);
        model.addAttribute("positionList",userList);
        return "sys/position/positionList";
    }



    //根据条件查询所有部门
    @ResponseBody
    @RequestMapping("/deptList")
    public List<DMDept> deptList(DMDept dmDept){
        return jmService.getDeptList(dmDept);
    }


    //搜索
    @RequestMapping("/queryPositions")
    public String queryDepts(Model model, JMJob jmJob, HttpSession session,PageModel<JMJob> pageModel){
        String trim = jmJob.getJob_name().trim();
        jmJob.setJob_name(trim);
        session.setAttribute("jmJob",jmJob);
        PageModel<JMJob> userList = jmService.getJMJobListByFY1(jmJob,pageModel);
        model.addAttribute("positionList",userList);
        return "sys/position/positionList1";
    }


    //职位名称局部验证
    @RequestMapping("/job_nameYZ")
    @ResponseBody
    public String job_nameYZ(JMJob jmJob){
        List<JMJob> dmDeptList = jmService.getJMJobListByJMJob(jmJob);
        if(dmDeptList!=null&&dmDeptList.size()>=1){
            return "true";
        }
        return "false";
    }


    //进入添加职位页面
    @RequestMapping("/positionAdd")
    public String positionAdd(){
        return "sys/position/positionAdd";
    }


    //添加职位
    @RequestMapping("/positionAdd1")
    @ResponseBody
    public String positionAdd1(JMJob jmJob){
        jmJob.setJob_state("0");
        Integer add = jmService.addJMJob(jmJob);
        if(add==1){
            return "true";
        }
        return "false";
    }



    //进入修改职位页面
    @RequestMapping("/positionUpdate")
    public String updateView(JMJob jmJob,Model model,HttpSession session){
        return jmService.updateView(jmJob,model,session);
    }


    //修改职位
    @RequestMapping("/updatePosition")
    @ResponseBody
    public String updatePosition(JMJob jmJob,HttpSession session){
        String job_id = (String) session.getAttribute("job_id");
        jmJob.setJob_id(job_id);
        Integer updateJMJob = jmService.updateJMJob(jmJob);
        if(updateJMJob==1){
            return "true";
        }
        return "false";
    }


    //修改职位状态
    @RequestMapping("/updateState")
    public String updateStatu(JMJob jmJob,Model model){
        return jmService.updateState(jmJob,model);
    }


    //修改职位状态(搜索后的修改)
    @RequestMapping("/updateState1")
    public String updateStatu1(JMJob jmJob,HttpSession session,Model model){
        JMJob dmDept1 = (JMJob) session.getAttribute("jmJob");
        return jmService.updateState1(jmJob,model,dmDept1);
    }


    //进入赋权页面
    @RequestMapping("/positionGrantView")
    public String positionGrantView(Permission permission,Model model,HttpSession session){
        return jmService.positionGrantView(permission,model,session);
    }

    //搜索后的进入赋权页面
    @RequestMapping("/positionGrantView1")
    public String positionGrantView1(Permission permission,Model model,HttpSession session){
        return jmService.positionGrantView1(permission,model,session);
    }


    //进入模块详情页
    @RequestMapping("/moduleView")
    public String moduleView(Module module,Model model){
        return jmService.moduleView(module,model);
    }


    //赋权
    @RequestMapping("/positionGrant")
    @ResponseBody
    public String positionGrant(@RequestBody String [] moduleGrant, HttpSession session, Model model){
        return jmService.addPermission(moduleGrant,session,model);
    }

    //赋权之后跳转该条记录所在页数页面
    @RequestMapping("/tz")
    public String tz(HttpSession session,Model model, JMJob jmJob, PageModel<JMJob> pageModel){
        String grandPageNum1 = (String) session.getAttribute("grandPageNum");
        int grandPageNum=Integer.parseInt(grandPageNum1);
        pageModel.setPageNum(grandPageNum);
        return userList(model, jmJob, pageModel);
    }

    //搜索后的赋权成功后跳转该条记录所在页数页面
    @RequestMapping("/tz1")
    public String tz1(HttpSession session,Model model, JMJob jmJob, PageModel<JMJob> pageModel){
        JMJob dmDept1 = (JMJob) session.getAttribute("jmJob");
        String grandPageNum1 = (String) session.getAttribute("grandPageNum");
        int grandPageNum=Integer.parseInt(grandPageNum1);
        pageModel.setPageNum(grandPageNum);
        PageModel<JMJob> userList = jmService.getJMJobListByFY(dmDept1,pageModel);
        model.addAttribute("positionList",userList);
        return "sys/position/positionList1";
    }


}
